package orm

import (
	"gorm.io/gorm"
	"hertz_demo/orm/db"
	"hertz_demo/orm/models"
)

func CommentVideo(user *models.User, videoId int64, content string) (models.Comment, error) {
	result := models.Comment{}
	dbComment := db.Comment{
		UserId:  user.ID,
		VideoId: videoId,
		Content: content,
	}
	err := db.DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&db.Comment{}).Create(&dbComment).Error; err != nil {
			return err
		}
		if err := tx.Model(&db.Video{}).Where("id=?", videoId).Update("comment_count", gorm.Expr("comment_count + ?", 1)).Error; err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return result, err
	}
	result.ID = dbComment.ID
	result.Creator = *user
	result.Content = dbComment.Content
	result.CreateDate = dbComment.CreatedAt.Format("01-02")
	return result, nil
}

func DeleteComment(user *models.User, videoId int64, commentId int64) error {
	err := db.DB.Transaction(func(tx *gorm.DB) error {
		commentCnt := 0
		if err := tx.Raw("select count(*) from `comments` where `id`=? and `video_id`=? and `user_id`=?", commentId, videoId, user.ID).Scan(&commentCnt).Error; err != nil {
			return err
		}
		if commentCnt == 0 {
			return gorm.ErrRecordNotFound
		}
		if err := tx.Model(&db.Comment{}).Where("id=?", commentId).Update("is_del", true).Error; err != nil {
			return err
		}
		if err := tx.Model(&db.Video{}).Where("id=?", videoId).Update("comment_cnt", gorm.Expr("comment_count - ?", 1)).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

func GetCommentList(currentUserId int64, videoId int64) ([]models.Comment, error) {
	var resultList []models.Comment
	var followList []int64
	if err := db.DB.Model(&db.FollowRelation{}).Select("follow_user_id").Where("user_id=?", currentUserId).Find(&followList).Error; err != nil {
		return resultList, err
	}
	var dbCommentList []db.Comment
	if err := db.DB.Model(&db.Comment{}).Where("`video_id`=? and `is_del`=0", videoId).Find(&dbCommentList).Error; err != nil {
		return resultList, err
	}
	var commentUserIdList []int64
	for _, dbComment := range dbCommentList {
		commentUserIdList = append(commentUserIdList, dbComment.UserId)
	}
	followList = GetIntersection(followList, commentUserIdList)
	var dbUserList []db.User
	if err := db.DB.Model(&db.User{}).Find(&dbUserList, commentUserIdList).Error; err != nil {
		return resultList, err
	}
	for _, dbComment := range dbCommentList {
		isFollow := false
		for _, id := range followList {
			if id == dbComment.UserId {
				isFollow = true
				break
			}
		}
		commentUser := models.User{
			ID:       dbComment.UserId,
			IsFollow: isFollow,
		}
		for _, dbUser := range dbUserList {
			if dbUser.ID == dbComment.UserId {
				commentUser.Name = dbUser.Name
				commentUser.FollowCount = dbUser.FollowCount
				commentUser.FollowerCount = dbUser.FollowerCount
				break
			}
		}
		comment := models.Comment{
			ID:         dbComment.ID,
			Creator:    commentUser,
			Content:    dbComment.Content,
			CreateDate: dbComment.CreatedAt.Format("01-02"),
		}
		resultList = append(resultList, comment)
	}
	return resultList, nil
}
